﻿// Decompiled with JetBrains decompiler
// Type: MCM.Abstractions.SettingsProviderWrapper
// Assembly: MCMv5, Version=5.10.2.0, Culture=neutral, PublicKeyToken=null
// MVID: 7AB326B9-B2B4-41C2-9177-68E944071101
// Assembly location: D:\steam\steamapps\workshop\content\261550\2859238197\bin\Win64_Shipping_Client\MCMv5.dll

using HarmonyLib.BUTR.Extensions;
using MCM.Abstractions.Base;
using MCM.Abstractions.Wrapper;
using MCM.Common;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;

#nullable enable
namespace MCM.Abstractions
{
  public abstract class SettingsProviderWrapper : BaseSettingsProvider, IWrapper
  {
    private readonly SettingsProviderWrapper.GetSettingsDefinitionsDelegate? _methodGetSettingsDefinitions;
    private readonly SettingsProviderWrapper.GetSettingsDelegate? _methodGetSettingsDelegate;
    private readonly SettingsProviderWrapper.SaveSettingsDelegate? _methodSaveSettingsDelegate;
    private readonly SettingsProviderWrapper.OverrideSettingsDelegate? _methodOverrideSettingsDelegate;
    private readonly SettingsProviderWrapper.ResetSettingsDelegate? _methodResetSettingsDelegate;

    public override IEnumerable<SettingsDefinition> SettingsDefinitions
    {
      get
      {
        SettingsProviderWrapper.GetSettingsDefinitionsDelegate settingsDefinitions = this._methodGetSettingsDefinitions;
        return (settingsDefinitions != null ? (IEnumerable<SettingsDefinition>) settingsDefinitions().Cast<object>().Select<object, SettingsDefinitionWrapper>((Func<object, SettingsDefinitionWrapper>) (x => new SettingsDefinitionWrapper(x))) : (IEnumerable<SettingsDefinition>) null) ?? (IEnumerable<SettingsDefinition>) Array.Empty<SettingsDefinitionWrapper>();
      }
    }

    public object Object { get; }

    protected SettingsProviderWrapper(object @object)
    {
      this.Object = @object;
      Type type = @object.GetType();
      this._methodGetSettingsDefinitions = AccessTools2.GetPropertyGetterDelegate<SettingsProviderWrapper.GetSettingsDefinitionsDelegate>(@object, type, nameof (SettingsDefinitions));
      this._methodGetSettingsDelegate = AccessTools2.GetDelegate<SettingsProviderWrapper.GetSettingsDelegate>(@object, type, "GetSettings");
      this._methodSaveSettingsDelegate = AccessTools2.GetDelegate<SettingsProviderWrapper.SaveSettingsDelegate>(@object, type, "SaveSettings");
      this._methodOverrideSettingsDelegate = AccessTools2.GetDelegate<SettingsProviderWrapper.OverrideSettingsDelegate>(@object, type, "OverrideSettings");
      this._methodResetSettingsDelegate = AccessTools2.GetDelegate<SettingsProviderWrapper.ResetSettingsDelegate>(@object, type, "ResetSettings");
    }

    protected abstract BaseSettings? Create(object obj);

    protected abstract bool IsSettings(BaseSettings settings, [NotNullWhen(true)] out object? wrapped);

    public override BaseSettings? GetSettings(string id)
    {
      SettingsProviderWrapper.GetSettingsDelegate settingsDelegate = this._methodGetSettingsDelegate;
      object obj = settingsDelegate != null ? settingsDelegate(id) : (object) null;
      return obj == null ? (BaseSettings) null : this.Create(obj);
    }

    public override void SaveSettings(BaseSettings settings)
    {
      object wrapped;
      if (!this.IsSettings(settings, out wrapped))
        return;
      SettingsProviderWrapper.SaveSettingsDelegate settingsDelegate = this._methodSaveSettingsDelegate;
      if (settingsDelegate == null)
        return;
      settingsDelegate(wrapped);
    }

    public override void OverrideSettings(BaseSettings settings)
    {
      object wrapped;
      if (!this.IsSettings(settings, out wrapped))
        return;
      SettingsProviderWrapper.OverrideSettingsDelegate settingsDelegate = this._methodOverrideSettingsDelegate;
      if (settingsDelegate == null)
        return;
      settingsDelegate(wrapped);
    }

    public override void ResetSettings(BaseSettings settings)
    {
      object wrapped;
      if (!this.IsSettings(settings, out wrapped))
        return;
      SettingsProviderWrapper.ResetSettingsDelegate settingsDelegate = this._methodResetSettingsDelegate;
      if (settingsDelegate == null)
        return;
      settingsDelegate(wrapped);
    }

    public abstract override IEnumerable<ISettingsPreset> GetPresets(string id);

    private delegate IEnumerable GetSettingsDefinitionsDelegate();

    private delegate object? GetSettingsDelegate(string id);

    private delegate void SaveSettingsDelegate(object settings);

    private delegate void OverrideSettingsDelegate(object settings);

    private delegate void ResetSettingsDelegate(object settings);
  }
}
